package com.cheys.webapp.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cheys.webapp.model.WebSite;
import com.cheys.webapp.service.AdminService;
import com.cheys.webapp.util.LogUtil;

import net.sf.json.JSONObject;
@Controller
@RequestMapping("/")
public class AbstractController {
	
	@Autowired
	private AdminService adminService;
	
	private Logger logger = LogUtil.getLogger();
	private static Map<String, WebSite> webSite = new HashMap<String, WebSite>();
	@RequestMapping("/getwebinfo")
	public void getwebinfo(HttpServletRequest request,HttpServletResponse response){
		logger.info("获取网站配置信息");
		try {
			logger.info("get webinfo from db");
			WebSite websiteInfo = webSite.get("webinfo");
			try {
				if(websiteInfo == null){
					websiteInfo = this.adminService.getWebsitInfo();
					webSite.put("webinfo", websiteInfo);
				}
			} catch (Exception e) {
				logger.error("get webinfo error,error message:"+e);
			}
			JSONObject json = JSONObject.fromObject(websiteInfo);
			logger.info("webinfo:"+json.toString());
			response.getWriter().write(json.toString());
		} catch (Exception e) {
			logger.debug("读取网站信息异常，请联系开发人员");
		}
	}
	/**
	 * 组建跳转页
	 * @param request
	 * @return
	 */
	@RequestMapping("/_index")
	public void toIndex(HttpServletRequest request,HttpServletResponse response){
		logger.info("组建跳转页……");
		try {
			logger.info("get webinfo from db");
			WebSite websitInfo = null;
			try {
				websitInfo = this.adminService.getWebsitInfo();
			} catch (Exception e) {
				logger.error("get webinfo error,error message:"+e);
			}
			if(websitInfo == null){
				logger.info("webinfo is null,request forward to index.do ");
				request.getRequestDispatcher("axxf/index.do");
			}else{
				String html = crateIndexHtml(this.adminService.getWebsitInfo());
				response.setCharacterEncoding("utf-8");
				response.getWriter().println(html);
			}
		} catch (Exception e) {
			logger.debug("读取网站信息异常，请联系开发人员");
		}
	}
	
	private String crateIndexHtml(WebSite webSite) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
//		FileHelper fileHelper = new FileHelper();
		try {
			ClassLoader classLoader = getClass().getClassLoader();
			sb.append("<!DOCTYPE html>\r\n");
			sb.append("<html lang=\"en\">\r\n");
			sb.append("<head>\r\n");
			sb.append("<meta charset=\"utf-8\">\r\n");
			sb.append("<title>"+webSite.getTitle()+"</title>\r\n");
			sb.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\r\n");
			sb.append("<meta name=\"description\" content=\""+webSite.getDescript()+"\" />\r\n");
			sb.append("<meta name=\"author\" content=\""+webSite.getOwner()+"\" />\r\n");
			sb.append("<meta name=\"keyworkds\" content=\""+webSite.getKeyword()+"\" />\r\n");
			String head = IOUtils.toString(classLoader.getResourceAsStream("indexhead.index"));
			sb.append(head);
		} catch (IOException e) {
			logger.debug("组建跳转页信息异常");
		}
		return sb.toString();
	}
}
